using UnityEngine;
using QFramework;
using System.Net.NetworkInformation;
using QFramework.Example;
using System;
namespace ProjectSurvivor
{
	public partial class EnemyMinBoss : ViewController, IEnemy
	{
		public enum States
		{
			FlowingPlayer,//移动
			Warning,//警戒状态
			Dash,//冲向主角
			Wait//等待
		}
		//有限状态机
		public FSM<States> FSM = new FSM<States>();
		//血量
		public float HP = 50;
		//移动速度
		public float MovementSpeed = 2.0f;
		public bool mIgnoreHurt = false;
		void Start()
		{
			//敌人生成数量++
			EnemyGenerator.EnemyCount.Value++;
			//有限状态机
			FSM.State(States.FlowingPlayer)
			//固定更新
			.OnFixedUpdate(() =>
			{
				//玩家存在
				if (Player.Default)
				{
					//计算方向
					var direction = (Player.Default.transform.position - transform.position).normalized;
					//向玩家移动
					SelfRigidbody2D.velocity = direction * MovementSpeed;
					//如果玩家到敌人的距离小于15
					if ((Player.Default.transform.Position() - transform.Position()).magnitude <= 15)
					{
						//警戒状态
						FSM.ChangeState(States.Warning);
					}
				}
				//玩家不存在
				else
				{
					//设置速度为0
					SelfRigidbody2D.velocity = Vector2.zero;
				}
			});
			//警戒状态
			FSM.State(States.Warning)
			//进入
			.OnEnter(() =>
			{
				//设置速度为0
				SelfRigidbody2D.velocity = Vector2.zero;
			})
			//更新
			.OnUpdate(() =>
			{
				//21-3  剩余的帧数，这个数值会随着时间推移而逐渐变小，/ 10 将其转化为更小的比例
				                           //当前状态的帧数
				var frames = 3 + (60 * 3 - FSM.FrameCountOfCurrentState) / 10;
				//如果当前帧是偶数帧
				if (FSM.FrameCountOfCurrentState / frames % 2 == 0)//偶数帧 
				{
					//变红
					Sprite.color = Color.red;
				}
				//如果当前帧是奇数帧
				else
				{
					//变白
					Sprite.color = Color.white;
				}
				//第几帧 一秒大概60帧
				if (FSM.FrameCountOfCurrentState >= 60 * 3)
				{
					//冲向主角
					FSM.ChangeState(States.Dash);
				}
			})
			//退出
			.OnExit(() =>
			{
				//变白
				Sprite.color = Color.white;
			});
			//冲向主角
			var dashStartPos = Vector3.zero;
			//与玩家的距离起始值
			var dashStartDistanceToPlayer = 0f;
			FSM.State(States.Dash)
			//进入
			.OnEnter(() =>
			{
				var direction = (Player.Default.Position() - transform.Position()).normalized;
				SelfRigidbody2D.velocity = direction * 15;
				dashStartPos = transform.Position();                                          //向量的长度（模长）
				dashStartDistanceToPlayer = (Player.Default.Position() - transform.Position()).magnitude;
			})
			//更新
			.OnUpdate(() =>
			{
				//计算距离
				var distance = (transform.Position() - dashStartPos).magnitude;
				//如果距离大于等于起始距离+5
				if (distance >= dashStartDistanceToPlayer + 5)
				{
					//等待状态
					FSM.ChangeState(States.Wait);
				}
			});
			//等待状态
			FSM.State(States.Wait)
			//进入
			.OnEnter(() =>
			{
				//设置速度为0
				SelfRigidbody2D.velocity = Vector2.zero;
			})
			//更新
			.OnUpdate(() =>
			{
				//如果当前帧大于等于30
				if (FSM.FrameCountOfCurrentState >= 30)
				{
					//移动状态
					FSM.ChangeState(States.FlowingPlayer);
				}

			});
			//开始状态
			FSM.StartState(States.FlowingPlayer);
		}
		//销毁
		void OnDestroy()
		{
			//敌人生成数量--
			EnemyGenerator.EnemyCount.Value--;
		}
		//固定更新
		void FixedUpdate()
		{
			//固定更新
			FSM.FixedUpdate();


		}
		private void Update()
		{
			FSM.Update();
			//血量为0
			if (HP <= 0)
			{
				//掉落
				Global.GeneratePowerUp(gameObject, true);
				//删除
				this.DestroyGameObjGracefully();
			}
		}
		//扣血
		public void Hurt(float value, bool force = false, bool critical = false)
		{
			//如果忽略伤害并且不是强制伤害
			if (mIgnoreHurt && !force) return;
			//飘血
			FloatingTextController.Play(transform.position, value.ToString("0"), critical);
			//变红
			Sprite.color = Color.red;
			//播放音效
			AudioKit.PlaySound("Hit");
			//延迟
			ActionKit.Delay(0.2f, () =>
			{
				//扣血
				HP -= value;
				//变白
				Sprite.color = Color.white;
				//设置忽略伤害为false
				mIgnoreHurt = false;
			}).Start(this);//开始
		}

		//设置血量缩放
		public void SetHPScale(float hPScale)
		{
			HP *= hPScale;
		}

		public void SetSpeedScale(float speedScale)
		{
			MovementSpeed *= speedScale;
		}
	}
}
